# -*- coding: utf-8 -*-
__author__ = 'hftec'

import math

voltage_array=[]
total_voltage_frame=[]
WAVELENGTH = 1.064
PI = 3.14158265
PI_2 = 6.2831653
from load_uphi_data import U_PHI
import lcopa_usb


# lcopausb = lcopa_usb.LcopaUsb()
# lcopausb.open(0x1235)



def getValuge(phi):
    min_index=0;
    min_error=10000;
    for cur_index in range(200):
        error = abs(U_PHI.uphi_struct.U_PHI[cur_index]-phi);
        if(error < min_error):
            min_error = error;#uphi.U_PHI[cur_index];
            min_index = cur_index;


    return min_index;

def getValugeFast():
    min_index=0
    # min_error=10000
    # cur_index = 100  #the middle index
    # while(True):
    #     if(phi<U_PHI.uphi_struct.U_PHI[cur_index]):
    #     error = abs(U_PHI.uphi_struct.U_PHI[cur_index]-phi);
    #     if(error < min_error):
    #         min_error = error;#uphi.U_PHI[cur_index];
    #         min_index = cur_index;
# return min_index;
'''
from voltage array calculate the ic frame code(voltage code for nt7708)
'''
def calculate_frame(vlo):
    #volate_frame = []
    total_volate_frame = []
    A_REG = 0
    B_REG = 0
    C_REG = 0
    D_REG = 0
    for i in range(8):
        total_volate_frame.append(0x00)

    total_volate_frame.append(0xaa)
    total_volate_frame.append(0xaa)
    for frame in range(200):
        volate_frame=[]
        for i in range(U_PHI.uphi_struct.ElectrodeNo):
            if(vlo[i] > (199-frame)):
                volate_frame.append(1)
            else:
                volate_frame.append(0)

        #DABC
        for j in range(60):
            pos = j<<4;
            for i in range(0,16,2):

                A_REG = A_REG<<1
                #A_REG += volate_frame[(j*16+i*2)];
                A_REG += volate_frame[958-(pos+i)]
                B_REG = B_REG<<1
                B_REG += volate_frame[(pos+1+i)]
                C_REG = C_REG<<1
                C_REG += volate_frame[(pos+961+i)]
                D_REG = D_REG<<1
                #//D_REG += volate_frame[(j*16+960+i*2)];
                D_REG += volate_frame[1918-(pos+i)]
            total_volate_frame.append(A_REG)
            total_volate_frame.append(D_REG)

            total_volate_frame.append(C_REG)
            total_volate_frame.append(B_REG)


            A_REG=0;
            B_REG=0;
            C_REG = 0;
            D_REG=0;
    #print "frame ok"
    total_bytes = ""
    for i in range(48010,48032):
        total_volate_frame.append(0x00)
    for i in range(len(total_volate_frame)):
        total_bytes+=(chr(total_volate_frame[i]))
    #print 'total_volate_frame',total_volate_frame
    return total_bytes
    #lcopausb.send_byte(total_bytes)
'''
calculate voltage by the angle
'''
def calculate_voltage(angle):
    volate_arry = [];

    k0 = 2 * PI / WAVELENGTH;
    d = (U_PHI.uphi_struct.ElectrodeWidth*1000000+U_PHI.uphi_struct.ElectrodeGap*1000000);
    deltaphi = k0 * d * math.sin((angle));

    for i in range(U_PHI.uphi_struct.ElectrodeNo):
        phi = ((deltaphi * i)%PI_2)
        #print("phi:%4f\r\n",phi);
        volate_arry.append(getValuge(phi))
        #print("len vol",len(volate_arry))

        # write_file("./u.txt",volate_arry,1920);
    return calculate_frame(volate_arry);
'''
calculate voltage by the angle
n_pi is the reset angle,seting by user
'''
def calculate_voltage_pi(angle,n_pi):
    volate_arry = [];

    k0 = 2 * PI / WAVELENGTH;
    d = (U_PHI.uphi_struct.ElectrodeWidth*1000000+U_PHI.uphi_struct.ElectrodeGap*1000000);
    deltaphi = k0 * d * math.sin((angle));

    for i in range(U_PHI.uphi_struct.ElectrodeNo):
        phi = ((deltaphi * i)%n_pi)
        #print("phi:%4f\r\n",phi);
        volate_arry.append(getValuge(phi))
        #print("len vol",len(volate_arry))

        # write_file("./u.txt",volate_arry,1920);
    return calculate_frame(volate_arry);

'''
calculate voltage by the angle
user can midiffy the calculated voltage by ramdom_val in index position
'''
def calculate_voltage_addoffet(angle,index,ramdom_val):
    volate_arry = []

    k0 = 2 * PI / WAVELENGTH
    d = (U_PHI.uphi_struct.ElectrodeWidth*1000000+U_PHI.uphi_struct.ElectrodeGap*1000000);
    deltaphi = k0 * d * math.sin((angle))

    for i in range(U_PHI.uphi_struct.ElectrodeNo):
        phi = ((deltaphi * i)%PI_2)
        #print("phi:%4f\r\n",phi);
        volate_arry.append(getValuge(phi))
        volate_arry[index] += ramdom_val   #add the random_val to modiffy the voltage
        #print("len vol",len(volate_arry))
        # write_file("./u.txt",volate_arry,1920);
    return calculate_frame(volate_arry)


def calculate_cmd(json_cmd):
    if(json_cmd["cmd"] == 2):#多个角度连续控制
        pass
    elif(json_cmd["cmd"] == 1):
        calculate_voltage(json_cmd["phi"])

